//
//  XMFilterTableCell.m
//  XMFilterView
//
//  Created by mac on 2019/11/21.
//  Copyright © 2019 aishangliguo. All rights reserved.
//

#import "XMFilterTableCell.h"

// View
#import "XMFilterCollectionCell.h"

static NSString *const cellID = @"cellID";

@interface XMFilterTableCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) NSInteger sctionCount;

@property (nonatomic, assign) CGFloat cotentViewHeight;

@end

@implementation XMFilterTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.filterTitle];
        [self.filterTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.contentView).offset(20);
        }];
        [self.contentView addSubview:self.filterButton];
        [self.filterButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView).offset(-20);
            make.centerY.mas_equalTo(self.filterTitle);
            make.height.offset(30);
        }];
        
        [self.contentView addSubview:self.filterCollectionView];
        [self.filterCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self.contentView).offset(0);
            make.top.mas_equalTo(self.filterButton.mas_bottom).offset(10);
        }];
        
        self.sctionCount = 3;
    }
    return self;
}

- (void)topClick:(UIButton *)sender {

    self.isFilterSelect = !self.isFilterSelect;
    
    if (self.isFilterSelect) {
        self.sctionCount = self.filterData.count;
    } else {
        self.sctionCount = 3;
    }
        
    [self.filterCollectionView layoutIfNeeded];
    
    [self.filterCollectionView reloadData];
    
//    NSLog(@"%f",self.cotentViewHeight);
//
//    if (self.delegate && [self.delegate respondsToSelector:@selector(cellFileterButton:)]) {
//        [self.delegate cellFileterButton:self];
//    }
}

// 重写计算UICollectionViewCell高度
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
    
    [super systemLayoutSizeFittingSize:targetSize withHorizontalFittingPriority:horizontalFittingPriority verticalFittingPriority:verticalFittingPriority];
    
    self.contentView.frame = CGRectMake(0, 0, targetSize.width, self.bounds.size.height);
    [self.contentView layoutIfNeeded];
    
    
    self.filterCollectionView.frame = CGRectMake(0, CGRectGetMaxY(self.filterButton.frame) + 10, targetSize.width, self.bounds.size.height - 60);
    [self.filterCollectionView layoutIfNeeded];
    
    CGSize collectionSize = self.filterCollectionView.collectionViewLayout.collectionViewContentSize;
    self.cotentViewHeight = collectionSize.height + 55;
    
    // 返回当前cell的高度
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, self.cotentViewHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",(long)indexPath.item);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.filterData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return section == 0 ? 3 : self.filterData.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(20, 20, 20, 20);
}



- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMFilterCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.collLabel.text = self.filterData[indexPath.item];
    return cell;
}

- (UICollectionView *)filterCollectionView {
    if (!_filterCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 15;
        flowLayout.minimumInteritemSpacing = 15;
        flowLayout.itemSize = CGSizeMake((kSCREEN_WIDTH - 114) / 3, 35);
        _filterCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _filterCollectionView.backgroundColor = [UIColor whiteColor];
        _filterCollectionView.delegate = self;
        _filterCollectionView.dataSource = self;
        [_filterCollectionView registerClass:[XMFilterCollectionCell class] forCellWithReuseIdentifier:cellID];
    }
    return _filterCollectionView;
}

- (UIButton *)filterButton {
    if (!_filterButton) {
        _filterButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _filterButton.backgroundColor = [UIColor blueColor];
        [_filterButton setTitle:@"上" forState:UIControlStateNormal];
        [_filterButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_filterButton addTarget:self action:@selector(topClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _filterButton;
}

- (UILabel *)filterTitle {
    if (!_filterTitle) {
        _filterTitle = [[UILabel alloc] init];
        _filterTitle.backgroundColor = [UIColor redColor];
        _filterTitle.textAlignment = NSTextAlignmentCenter;
    }
    return _filterTitle;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
